package ajax.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;


public class DBUtils {
	private static Connection conn = null;
	public static Connection getConnnection(){
		try {
			Context ic = new InitialContext();
			DataSource source = (DataSource) ic
					.lookup("java:comp/env/jdbc/restrant");
			// 通过JNDI读取的数据源建立数据库链接
			conn = source.getConnection();
		} catch (Exception e) {
			System.out.println("数据库登陆失败2");
			e.printStackTrace();
		}
		return conn;
	}
	public static void close(ResultSet rs){
		try {
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public static void close(PreparedStatement pstatement){
		try {
			pstatement.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public static void close(Connection conn){
		try {
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	

}
